﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;


namespace zhihu {
    public partial class FormMain : Form {
        public FormMain() {
            InitializeComponent();
        }

        private void btnSignUp_Click(object sender, EventArgs e) {
            FormSignUp fSignUp = new FormSignUp();
            if (DialogResult.OK != fSignUp.ShowDialog()) return;

            switch (fSignUp.SignUpMode) {
                case SignUpDialogResult.SetCookies:
                    // 设置COOKIES模式
                    FormSetCookie fSetCookie = new FormSetCookie();
                    fSetCookie.ShowDialog();
                    break;
                case SignUpDialogResult.WebSignUp:
                    // 网页登陆模式
                    FormWebSignUp fWebSignUp = new FormWebSignUp();
                    fWebSignUp.ShowDialog();
                    break;
                case SignUpDialogResult.Default:
                    // 正常情况下是不会进入这个分支的
                    MessageBox.Show("没有选择登陆方式.");
                    break;
                default:
                    MessageBox.Show("选择登陆模式,未知错误.");
                    break;
            }
        }

        private void btnStart_Click(object sender, EventArgs e) {
            if (string.IsNullOrEmpty(GlobalInfo.LoginCookies)) {
                MessageBox.Show(@"还没登陆知乎!");
                return;
            }
            TaskFunction task = TaskFunction.GetTaskFunction();
            if (task.IsFirst()){
                task.ShowMsg += OnShowMsg;
            }
            task.Start();
        }

        #region 输出消息OnShowMsg
        private void OnShowMsg(object sender, TaskEventArgs e) {
            this.Invoke(new MethodInvoker(() => {
                if (lbxLog.Items.Count > 100) {
                    lbxLog.Items.Clear();
                }
                int nCurItem = lbxLog.Items.Add(e.MessageString);
                lbxLog.SetSelected(nCurItem, true);
            }));
        } 
        #endregion

        private void btnStop_Click(object sender, EventArgs e){
            

            //MessageBox.Show(@"ok");
            TaskFunction task = TaskFunction.GetTaskFunction();
            task.Stop();
        }

        private void btnSetting_Click(object sender, EventArgs e) {
            FormSetting fSetting = new FormSetting();
            fSetting.ShowDialog();
        }

        private void btnExport_Click(object sender, EventArgs e) {
            //List<ViewInfo> listViewInfo = ZhihuHelper.GetTopOfDataBase(GlobalInfo.ExportCount);

            //SaveFileDialog sfdlg = new SaveFileDialog();
            //sfdlg.InitialDirectory = Application.StartupPath;
            //sfdlg.Filter = @"网页文件|*.html|所有文件|*.*";
            //sfdlg.FilterIndex = 0;
            //if (sfdlg.ShowDialog() == DialogResult.OK) {
            //    ExportHelper.Expore(sfdlg.FileName, listViewInfo);
            //}
            FormExport frmExp = new FormExport();
            frmExp.ShowDialog();
        }

        
    }
    
}
